package com.itheima.grow.service.impl;

import com.itheima.grow.model.enums.TemplateNameEnum;
import com.itheima.grow.model.info.OssFileInfo;
import com.itheima.grow.service.IPdfService;
import com.itheima.grow.service.OssService;
import com.itheima.grow.util.DateUtil;
import com.itheima.grow.util.PdfHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;

@Service
@Slf4j
public class PdfServiceImpl implements IPdfService {

    @Resource(name = "asyncServiceExecutor")
    private Executor executor;

    @Autowired
    private PdfHelper pdfHelper;

    @Resource
    private OssService ossService;

    @Override
    public OssFileInfo generatePdf() throws Exception {
        Map<String, Object> pdfMap = getPdfMap();
        byte[] pdfLang = null;
        pdfLang = pdfHelper.createPDF(pdfMap, TemplateNameEnum.CREDIT_OR_DEBIT_INVOICE_TEMPLATE.getCode());
        OssFileInfo fileInfo = ossService.uploadFile(pdfLang);
        return fileInfo;
    }

    @Override
    public String test() {
        //打印当前线程名称
        System.out.println("before:"+Thread.currentThread().getName());
        for (int i = 0; i < 100; i++) {
            executor.execute(()->{
                try {
                    //打印当前线程名称
                    System.out.println(Thread.currentThread().getName());
                    //休息1秒这样的话这个线程应该用时100s
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
        }
        return "end";
    }

    public Map<String, Object> getPdfMap(){
        Map<String, Object> map = new HashMap<>();
        map.put("company", "Oracle");
        map.put("street", "California");
        map.put("postalCode", "102010");
        map.put("city", "CHANGCHUN");
        map.put("province", "JILIN");
        map.put("country", "CHIAN");
        map.put("areaCode", "G001");
        map.put("telephone", "15779587011");
        map.put("faxAreaCode", "G002");
        map.put("email", "157163827@qq.com");
        map.put("officialWebsite", "www.dz.com");
        map.put("faxNumber", "1");
        map.put("registeredOffice", "California");
        map.put("vatRegNo", "001");
        map.put("iban", "COCO");
        map.put("bic", "COCO");
        map.put("title", "INVOICE");
        map.put("customerName","Oracle");
        map.put("customerNo","Oracle001");
        map.put("customerVat", "KJ CHINA");
        map.put("yourReference", "California 236Number");
        map.put("vin", "VIN987654321");
        map.put("customerAddress", "California 236Number");
        map.put("paymentType", "Online");
        map.put("paymentDate", DateUtil.formatDate(new Date(),DateUtil.FORMAT_5));
        map.put("licensePlate", "Online");
        map.put("invoiceNo", "LOTUS2023121101");
        map.put("invoiceDate", DateUtil.formatDate(new Date(),DateUtil.FORMAT_5));
        map.put("serviceType", "Credit Memo");
        map.put("currency", "EU");
        map.put("Quantity", "1");
        map.put("priceExVat", "1.2");
        map.put("vatRate", "0.8");
        map.put("vatAmount",  "99");
        map.put("totalExVat","88");
        map.put("totalVat","33");
        map.put("totalInclVat", "66");
        map.put("titleLang","XTS");
        map.put("lang", "DE");
        map.put("footerFirstLang", "DE");
        map.put("officeLang", "DE");
        map.put("quantityLang", "DE");
        map.put("type", "DE");
        return  map;
    }
}
